package com.patchmanage.back.controller;


import cn.hutool.core.lang.Dict;
import com.patchmanage.back.entity.GeneralConfiguration;
import com.patchmanage.back.result.Result;
import com.patchmanage.back.service.IGeneralConfigurationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.time.LocalDateTime;
import java.util.Date;

/**
 *
 * @author huizhao
 * @since 2021-04-14
 */
@Api(value = " 通用配置 controller", tags = {"通用配置 Api"})
@RestController
@RequestMapping("/back/generalconfiguration")
public class GeneralConfigurationController {

    @Autowired
    public IGeneralConfigurationService generalConfigurationService;

    /**
     * 获取通用配置接口
     *
     * @param dict
     * @return
     */
    @ApiOperation(value = "获取通用配置更新接口", notes = "获取通用配置更新接口")
    @RequestMapping(value = "getById", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Integer", paramType = "body")
    })
    public Result<GeneralConfiguration> getById(@ApiIgnore @RequestBody Dict dict) {
        return Result.success(generalConfigurationService.getById(dict.getInt("id")));
    }


    /**
     * 新增通用配置更新接口
     * @param generalConfiguration
     * @return
     */
    @ApiOperation(value = "新增通用配置更新接口", notes = "新增通用配置更新接口")
    @RequestMapping(value = "saveOrUpdate", method = RequestMethod.POST)
    public Result<Boolean> saveOrUpdate(@RequestBody GeneralConfiguration generalConfiguration){
        return Result.success(generalConfigurationService.saveOrUpdate(generalConfiguration));
    }

    /**
     * 获取系统当前时间
     * @param generalConfiguration
     * @return
     */
    @ApiOperation(value = "获取系统当前时间", notes = "获取系统当前时间")
    @RequestMapping(value = "getServerTime", method = RequestMethod.POST)
    public Result<Object> getServerTime(@RequestBody GeneralConfiguration generalConfiguration){
        return Result.success(LocalDateTime.now());
    }

    /**
     * 从NTP服务器同步系统时间
     * @param dict
     * @return
     */
    @ApiOperation(value = "从NTP服务器同步系统时间", notes = "从NTP服务器同步系统时间")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ntpServerUrl", value = "ntpServerUrl", required = true, dataType = "String", paramType = "body")
    })
    @RequestMapping(value = "syncTimeFromNtpServer", method = RequestMethod.POST)
    public Result<Boolean> syncTimeFromNtpServer(@ApiIgnore @RequestBody Dict dict){

        /**从ntp获取时间同步本地系统**/
        String ntpServerUrl = dict.getStr("ntpServerUrl");


        /**更新配置**/
        GeneralConfiguration g = new GeneralConfiguration();
        g.setId(1);
        g.setSyncTimeNtpServerUrl(ntpServerUrl);
        return Result.success(generalConfigurationService.updateById(g));
    }

    /**
     * 修改系统时间
     * @param dict
     * @return
     */
    @ApiOperation(value = "修改系统时间", notes = "修改系统时间")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "newSystemTime", value = "newSystemTime", required = true, dataType = "date", paramType = "body")
    })
    @RequestMapping(value = "updateSystemTime", method = RequestMethod.POST)
    public Result<Boolean> updateSystemTime(@ApiIgnore @RequestBody Dict dict){

        /**修改系统时间**/
        Date newSystemTime = dict.getDate("newSystemTime");

        return Result.success(true);
    }

    /**
     * 系统运行
     * @param dict
     * @return
     */
    @ApiOperation(value = "系统运行", notes = "系统运行")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "serviceType(1.WEB应用服务,2系统主服务,3网卡服务,4平台主机服务)", value = "serviceType", required = true, dataType = "Integer", paramType = "body"),
            @ApiImplicitParam(name = "operation(1.重启，2关机)", value = "serviceType", required = true, dataType = "Integer", paramType = "body")
    })
    @RequestMapping(value = "systemOperation", method = RequestMethod.POST)
    public Result<Boolean> systemOperation(@ApiIgnore @RequestBody Dict dict){

        Integer serviceType = dict.getInt("serviceType");
        Integer operation = dict.getInt("serviceType");

        /**调用脚本或者接口**/

        return Result.success(true);
    }
}
